package com.ybkj.daijia.server.scheduler;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.DriverTimeService;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author FZH
 */
@Component
public class WorkTime {

    private static final Logger logger = LoggerFactory.getLogger(WorkTime.class);

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private DriverService driverService;

    @Autowired
    private DriverTimeService driverTimeService;

    @Transactional(rollbackFor = Exception.class)
    public void check() {

        Date now = new Date();

        List<Driver> drivers = driverService.listValid(null);
        if (!CollectionUtils.isEmpty(drivers)) {
            for (Driver driver : drivers) {

                Calendar c = Calendar.getInstance();
                c.setTime(now);

                driverTimeService.driverTimeOnline(driver, c);
                c.add(Calendar.DATE, -1);
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                driverTimeService.driverTimeOffline(driver, c);

            }
        }

    }

}
